package edu.qqilin.springframework.beans.factory.support;

import edu.qqilin.springframework.beans.BeansException;
import edu.qqilin.springframework.beans.factory.config.BeanDefinition;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Description:  JDK 实例化
 * <p>
 * 使用 Constructor.newInstance()。
 * @author qqilin1213
 * @version 1.0
 * @date 2025/11/23 14:06
 */
public class SimpleInstantiationStrategy implements InstantiationStrategy {
    @Override
    public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException {
        Class<?> clazz = beanDefinition.getClass();
        try{
            if(null != ctor){
                return clazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args);
            } else{
                return clazz.getDeclaredConstructor().newInstance();
            }
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}
